// pages/map/map.ts
export { }
const app: any = getApp<IAppOption>()
const { $http } = app.globalData
var startmark: any = 0;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        markers: [],
        page: 1,
        maplist: [],
        value: "",
        longitude: "117.158833",
        latitude: "31.835198",
        topvalue: 30,
        iconpathurl: 'https://sg.orthok.cn/image/wxapp'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        const mapcontext: any = wx.createMapContext('map', this)
        mapcontext.setCenterOffset({
            offset: [0.5, 0.2]
        })

    },
    // 跳转定位
    moveToLocation() {
        const mapcontext: any = wx.createMapContext('map', this)
        mapcontext.moveToLocation()
    },
    /**
       * 生命周期函数--监听页面显示
       */
    mapTo(e: any = 0) {
        if (e != 0) {
            var list: any = this.data.maplist[e.currentTarget.dataset.value]
        } else {
            var list: any = this.data.maplist[0]
        }
        const mapcontext: any = wx.createMapContext('map', this)
        console.log(mapcontext)
        mapcontext.moveToLocation({
            latitude: list.latitude,
            longitude: list.longitude,

        })
    },
    onfocus(e: any) {
        if (e <= 0) {
            this.setData({
                topvalue: 70
            })
        }
    },

    touchstart(e: any,) {
        var pageY = (e.touches[0] || e.changedTouches[0]).pageY;
        startmark = pageY;
    },
    touchmove() {
    },
    touchend(e: any) {
        var pageY = (e.touches[0] || e.changedTouches[0]).pageY;
        var offset = pageY - startmark;
        this.onfocus(Math.min(offset))

    },
    scrolltolower() {
        this.setData({
            page: this.data.page + 1
        })
        if ((this.data.page - 1) * 10 == this.data.maplist.length) {
            this.shopget()
        }

    },
    onshowfocus() {
        if (this.data.topvalue != 30) {
            this.setData({
                topvalue: 30
            })
        }
    },
    openLocation(e: any) {
        var list: any = this.data.maplist[e.currentTarget.dataset.value]
        wx.openLocation({
            latitude: list.latitude,
            longitude: list.longitude
        })
    },

    appoinTo(e: any) {
        console.log(e)
        const appoint_info: any = this.data.maplist[e.currentTarget.dataset.index]
        wx.setStorageSync("appoint_info", appoint_info)
        wx.navigateTo({
            url: '../appointment/appointment?mendiantitle=' + appoint_info.shopName
        })
    },
    onShow() {
        wx.getLocation({
            type: 'wgs84',
            success: async (res: any) => {
                console.log(res)
                this.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                })
                //    判断是否缓存
                if (this.data.maplist.length == 0) {
                    this.setData({
                        maplist: [],
                        page: 1
                    })
                    this.shopget()
                }
            },
            fail(e) {
                console.error(e)
            }
        })

    },
    async shopget() {
        const mapcontext: any = wx.createMapContext('map', this)
        var mapid: any = this.data.markers
        if (mapid.length != 0) {
            console.log(mapid.flatMap((e: any) => { return e.id }))
            mapcontext.removeMarkers({
                markerIds: mapid.flatMap((e: any) => { return e.id })
            })
        }
        mapcontext.initMarkerCluster()
        let rev: any = await $http.index.shopget({
            longitude: this.data.longitude,//,//res.longitude,
            latitude: this.data.latitude,//"31.835198",//res.latitude,
            page: this.data.page,
            keyWordsStr: this.data.value
        })
        if (rev.statusCode == 200) {
            delete rev.statusCode
            var markers: any = []
            rev.forEach((e: any, index: any) => {
                console.log(e)
                markers.push({
                    id: index,
                    width: "60rpx",
                    height: "60rpx",
                    longitude: e.longitude,
                    latitude: e.latitude,
                    iconPath: '/image/icon/401wz.png',
                    label: {
                        content: e.shortName,
                        color: '#888888',
                        borderRadius: '5',
                        anchorY: '-60px',
                        padding: "10rpx",
                        bgColor: "#fff",
                        textAlign: 'center'
                    }
                })
            })
            console.log(markers)

            mapcontext.addMarkers({
                markers: markers
            })
            if (rev.length != 0) {
                mapcontext.moveToLocation({
                    latitude: rev[0].latitude,
                    longitude: rev[0].longitude,

                })
            }
            var lis: any = [...this.data.maplist, ...rev]
            this.setData({
                maplist: lis,
                markers: markers
            })
        }

    },
    // 点击定位标点
    bindmarkertap(e: any) {
        wx.setStorageSync("mapinfo", this.data.maplist[e.detail.markerId])
        wx.navigateTo({
            url: './shopinfo/shopinfo'
        })
    },
    searchinput(e: any) {
        this.setData({
            value: e.detail
        })
    },
    onSearch() {
        this.setData({
            page: 1,
            maplist: [],
        })
        this.shopget()
    },

    tapSalon(e: any) {
        console.info("shopinfoTo")
        wx.setStorageSync("mapinfo", this.data.maplist[e.currentTarget.dataset.value])
        wx.navigateTo({
            url: '../salon/salon'
        })
    }
    ,
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作s
     */
    onPullDownRefresh() {
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {
    },
    dianhua(e: any) {
        console.log(this)
        var that: any = this
        wx.makePhoneCall({
            phoneNumber: that.data.shopinfo.shopPhone
        })
    },
    dizhi() {
        var that: any = this
        wx.openLocation({
            longitude: that.data.shopinfo.longitude,//纬度     必填
            latitude: that.data.shopinfo.latitude,//经度     必填
            address: that.data.shopinfo.shopAddress,//地址的详细说明
            name: that.data.shopinfo.shopName//位置名
        })

    },
})